using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using ORS.OrderRobot.Service;

namespace ORS.OrderRobot.WebApp.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ChatController : ControllerBase
    {
        private readonly IHubContext<MessageHub> _hubContext;

        public ChatController(IHubContext<MessageHub> hubContext)
        {
            _hubContext = hubContext;
        }

        [HttpPost("send")]
        public async Task<IActionResult> SendMessage([FromBody] MessageRequest request)
        {
            try
            {
                await _hubContext.Clients.All.SendAsync("ReceiveMessage", request.Message, request.MessageType ?? "info");
                return Ok(new { success = true, message = "消息发送成功" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = $"发送失败: {ex.Message}" });
            }
        }

        [HttpGet("connect")]
        public IActionResult Connect()
        {
            return Ok(new { 
                success = true, 
                message = "SignalR Hub 连接正常",
                hubUrl = "/log",
                timestamp = DateTime.Now
            });
        }
    }

    public class MessageRequest
    {
        public string Message { get; set; } = "测试消息";
        public string? MessageType { get; set; } = "info";
    }
} 